package cn.iocoder.yudao.module.engine.util;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.engine.enums.SensorTypeEnum;
import cn.iocoder.yudao.module.engine.model.engine.EngineInputData;

import java.util.List;

public class SensorUtils {

    public static EngineInputData.Sensor getMaxPrioritySensor(List<EngineInputData.Sensor> sensors) {
        // 优先级值越小的，优先级越高。
        // 从sensors中查找优先级最高的sensor
        Integer targetPriority = null;
        EngineInputData.Sensor targetSensor = null;
        for (EngineInputData.Sensor sensor : sensors) {
            final SensorTypeEnum sensorTypeEnum = SensorTypeEnum.fromValue(sensor.getSensorType());
            final int priority = sensorTypeEnum.getPriority();
            if (targetPriority == null || priority < targetPriority) {
                targetPriority = priority;
                targetSensor = sensor;
            }
        }
        return targetSensor;
    }

    public static EngineInputData.Sensor getSensor(List<EngineInputData.Sensor> sensors, SensorTypeEnum targetType) {
        if (CollUtil.isEmpty(sensors)) {
            return null;
        }

        EngineInputData.Sensor targetSensor = null;
        for (EngineInputData.Sensor sensor : sensors) {
            final SensorTypeEnum sensorTypeEnum = SensorTypeEnum.fromValue(sensor.getSensorType());
            if (targetType == sensorTypeEnum) {
                targetSensor = sensor;
                break;
            }
        }
        return targetSensor;
    }
}
